package com.huike.business.service.impl;


import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public List<TbBusinessTrackRecord> selectTbBusinessTrackRecordList(Long businessId) {
        List<TbBusinessTrackRecord> tbBusinessTrackRecordList = tbBusinessTrackRecordMapper.selectTbBusinessTrackRecordList(businessId);
        tbBusinessTrackRecordList = tbBusinessTrackRecordList.stream().map(tbBusinessTrackRecord -> {
            String keyItems = tbBusinessTrackRecord.getKeyItems();
            List<String> keys = Arrays
                    .stream(keyItems.split(","))
                    .map(String::trim)
                    .map(value ->
                            sysDictDataMapper.selectDictLabel(TbBusinessTrackRecord.DictType.KEYITEMS.getDictType(), value)
                    ).collect(Collectors.toList());
            tbBusinessTrackRecord.setKeys(keys);
            return tbBusinessTrackRecord;
        }).collect(Collectors.toList());
        return tbBusinessTrackRecordList;
    }

    @Override
    public int insertTbBusinessTrackRecord(TbBusinessTrackRecord tbBusinessTrackRecord) {
        tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());

        return tbBusinessTrackRecordMapper.insertTbBusinessTrackRecord(tbBusinessTrackRecord);

    }


}
